package cn.tworice.admin.web;

import cn.tworice.common.inducts.InductsListener;
import cn.tworice.admin.service.AdminService;
import cn.tworice.admin.service.LogService;
import cn.tworice.common.entity.Admin;
import cn.tworice.common.vo.RequestResult;
import cn.tworice.common.vo.StatusCode;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/4 20:16
 */
@RestController
@RequestMapping("admin/admin")
@Slf4j
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService service;

    @Autowired
    private LogService logService;

    @GetMapping("list")
    @ApiOperation("获取管理员列表")
    public RequestResult list(@ApiParam("页码") Integer page,@ApiParam("每页数量") Integer pageSize,Admin entity) {
        RequestResult result = new RequestResult();
        result.appendData("list", service.getAdminList(page,pageSize,entity));
        result.appendData("total",service.getAdminTotal(entity));
        result.setStatus(new StatusCode(200, "获取成功"));
        return result;
    }

    @PostMapping("add")
    @ApiOperation("添加一个管理员账号")
    public RequestResult add(Admin admin, HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"添加/更新一个人员账号信息");
        RequestResult result = new RequestResult();
        service.addAdmin(admin);
        result.setStatus(new StatusCode(200, "添加一个管理员账号，返回添加账号后的列表"));
        return result;
    }

    @DeleteMapping("del")
    @ApiOperation("删除人员账号")
    public RequestResult del(String[] ids,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"删除人员账号-"+ Arrays.toString(ids));
        RequestResult result = new RequestResult();
        service.delAdminByIds(ids);
        result.setStatus(new StatusCode(200,"删除人员账号"));
        return result;
    }

    @PutMapping("update")
    @ApiOperation("更新管理员密码")
    public RequestResult update(Admin admin,HttpServletRequest request){
        logService.addAdminLog(request.getHeader("adminID"),"更新账号密码");
        RequestResult result = new RequestResult();
        boolean b=service.updateAdminInfo(admin);
        result.setStatus(new StatusCode(200,"更新人员信息"));
        result.appendData("status", b);

        return result;
    }

    @PostMapping("inducts")
    @ApiOperation("通过Excel表格批量上传数据")
    public RequestResult inducts(MultipartFile file) {
        RequestResult result = new RequestResult();
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, Admin.class, new InductsListener(service)).sheet().doRead();
            result.setStatus(new StatusCode(200,"批量导入数据成功"));
        }catch (Exception ex){
            ex.printStackTrace();
            result.setStatus(new StatusCode(204,"批量导入数据失败"));
        }
        return result;
    }

    /**
     *
     * @param response
     * @param entity
     * @throws IOException
     */
    @GetMapping("template")
    @ApiOperation("获取Excel模板")
    public void template(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Admin.class).sheet("模板").doWrite(new ArrayList<>());
    }

    @GetMapping("info")
    @ApiOperation("通过ID获取数据")
    public RequestResult info(String id){
        RequestResult result = new RequestResult();
        Admin admin=service.getInfo(id);
        result.appendData("info",admin);
        return result;
    }
}
